package com.lg.system.wxconfig.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.wxconfig.entity.WxConfig;
import com.lg.system.wxconfig.service.WxConfigService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-03
 */
@RestController
@RequestMapping("/wxconfig")
public class WxConfigController extends BaseController<WxConfig, WxConfigService> {

    private final String prefix="system/wxconfig/";
    
	@Autowired
	WxConfigService wxConfigService;

	@RequiresPermissions("wxconfig:wxconfig")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("wxconfig:wxconfig")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody WxConfig  wxConfig){
		Query<WxConfig> query = new Query<WxConfig>(wxConfig);
		List<WxConfig> wxConfigs=wxConfigService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(wxConfigs,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("wxconfig:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("wxconfig:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", wxConfigService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("wxconfig:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) WxConfig wxConfig,BindingResult bindingResult){
		if(wxConfigService.save(wxConfig)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxconfig:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) WxConfig wxConfig,BindingResult bindingResult){
		if(wxConfigService.update(wxConfig)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxconfig:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) WxConfig wxConfig,BindingResult bindingResult){
		
		if(wxConfigService.deleteById(wxConfig.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("wxconfig:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(wxConfigService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}

}
